/**
 * 
 */
package com.lu.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.lu.dto.LogDto;
import com.lu.model.Log;
import com.lu.model.MyPrincipal;
import com.lu.persistence.dao.LogMapper;
import com.lu.service.ILogService;

/**
 * @author lusm
 *
 * @Date 2016年4月30日
 */
@Service("logService")
public class LogServiceImpl implements ILogService {
	private long processTime;
	@Resource
	private LogMapper logMapper;

	@Override
	public void beforeRequest(JoinPoint joinpoint) {

	}

	@Override
	public Object aroundRequest(ProceedingJoinPoint joinpoint) throws Throwable {
		long time = System.currentTimeMillis();
		Object pro = joinpoint.proceed();
		processTime = System.currentTimeMillis() - time;
		return pro;
	}

	@Override
	public void afterRequest(JoinPoint joinpoint) {
		Log log = new Log();
		log.setTimeUsed(processTime);
		log.setMemo("调用" + joinpoint.getTarget().toString() + "  Controller的" + joinpoint.getSignature().getName() + "方法");
		Subject subject = SecurityUtils.getSubject();
		MyPrincipal principal = (MyPrincipal) subject.getPrincipal();
		if (principal != null && principal.getUser() != null) {
			log.setUserId(principal.getUser().getId());
		}
		logMapper.insert(log);
	}

	@Override
	public List<LogDto> getLogList(int page, int rows) {
		PageHelper.startPage(page, rows);
		List<LogDto> list = logMapper.selectList();
		return list;
	}

}
